import { Component, OnInit } from '@angular/core';
import { HttpRequest } from "../../../service/httpRequest";
import { FileLoader } from "../../../service/fileLoader";
import { flyIn } from '../../../animations/fly-in';

declare var $:any;

@Component({
  selector: 'app-user-info',
  templateUrl: './user-info.component.html',
  styleUrls: ['./user-info.component.css'],
  animations: [
    flyIn
  ]
})
export class UserInfoComponent implements OnInit {
  modify:boolean = false;
  data:any;
  modifyData = {nick_name:'',email:'',mobile:'',remark:'',facing_img:''};

  constructor(private httpRequest:HttpRequest,private fileLoader:FileLoader) { }

  ngOnInit() {
    this.fileLoader.LoadFileList(['assets/js/uploadify/jquery.uploadify.min.js'],()=>{
      let that =this;
      $('#file_upload').uploadify({
        'swf'      : 'assets/js/uploadify/uploadify.swf',
        'uploader' : "http://101.37.24.73:8080/fileupload/",
        'buttonText': '上传账户图片',
        'fileObjName' : 'file',
        'fileTypeDesc' : '选择文件',
        'fileTypeExts' : '*.jpg;*.png;',
        'method' : 'post',
        'fileSizeLimit':'10MB',
        'auto' : true,
        'multi' : false,
        'onUploadSuccess' : (file,data,response) => {
          let filedata = JSON.parse(data);
          that.modifyData.facing_img = filedata.file_path;
          $("#head_img").attr('src','http://121.196.221.74:8080/fileupload/mfile?file_name='+filedata.file_path);
          $("#file_uploaded").text(filedata.file_path+"上传完成");
        }
      });

    });
    this.getData();
  }

  getData(){
    this.httpRequest.sendGet('account').subscribe(res => {
      this.data = res.result;
    },err => console.log(err))
  }

  init(){
    $("#file_uploaded").text('');
    this.modifyData.nick_name =this.data.nick_name ? this.data.nick_name : '';
    this.modifyData.email =this.data.email ? this.data.email : '';
    this.modifyData.mobile =this.data.mobile ? this.data.mobile : '';
    this.modifyData.remark =this.data.remark ? this.data.remark : '';
    this.modifyData.facing_img = this.data.facing_img ? this.data.facing_img : '';
    $("#head_img").attr('src','http://121.196.221.74:8080/fileupload/mfile?file_name='+this.data.facing_img);
    this.modify=true;
  }

  clean(){

    this.modifyData = {nick_name:'',email:'',mobile:'',remark:'',facing_img:this.data.facing_img};
  }

  save(){
    this.httpRequest.sendPost('account/'+this.data.id,this.modifyData).subscribe(res=>{
      alert("用户信息修改成功！");
      this.getData();
      //JSON.parse(localStorage.getItem('account')).head_img = this.data.facing_img;
      this.modify = false;
    },err => {alert('用户信息修改失败!');console.log(err)});
  }


}
